package datastructure.book.dp._2_linear_pre_section.single.all;

import org.junit.jupiter.api.Test;

public class _15_Solution {

    public int shortestWay(String source, String target) {
        char[] sourceCharArray = source.toCharArray();
        char[] targetCharArray = target.toCharArray();
        StringBuilder sb = new StringBuilder();
        int preIndex = -1;
        int result = 1;
        for (int i = 0; i < targetCharArray.length; i++) {
            char tarChar = targetCharArray[i];
            int index = findTarChar(sourceCharArray,tarChar,preIndex+1);
            if (index == -1) {
                index = findTarChar(sourceCharArray,tarChar,0);
                if (index == -1) {
                    return -1;
                }
                sb.append(",").append(tarChar);
                result++;
                preIndex = index;
                continue;
            }
            sb.append(tarChar);
            preIndex = index;
        }
        System.out.println(sb);
        return result;
    }

    private int findTarChar(char[] sourceCharArray,char tarChar,int start) {
        for (int i = start; i < sourceCharArray.length; i++) {
            if (sourceCharArray[i] == tarChar) {
                return i;
            }
        }
        return -1;
    }

    @Test
    public void test(){
        System.out.println(shortestWay("abc", "abcbc"));
    }
}
